/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ch3.demo.jsf;

import java.io.Serializable;
import java.util.ArrayList;
import javax.enterprise.context.Conversation;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
@Named
@SessionScoped
public class QuizBean implements Serializable {

   private ArrayList<ProblemBean> problems = new ArrayList<ProblemBean>();     
   private int currentIndex;
  
   private int score;
   @Inject Conversation conversation;
   public QuizBean() {      
      problems.add(
         new ProblemBean("制造日期与有效日期是同一天的产品是什么？" ,"报纸" )); 
      problems.add(
         new ProblemBean("什么东西肥得快，瘦得更快 ？" ,"气球" ));
      problems.add(
        new ProblemBean("放一支铅笔在地上,要使任何人都无法跨过,怎么做?" ,"放在墙边" ));
      problems.add(
         new ProblemBean("青蛙为什么能跳得比树高？" ,"树不会跳" ));
      problems.add(
        new ProblemBean("最不听话的是谁？" ,"聋子" ));
   }   

   public void setProblems(ArrayList<ProblemBean> newValue) { 
      problems = newValue;
      currentIndex = 0;
    
   } 
      
   public int getScore() { return score; }

   public ProblemBean getCurrent() { return problems.get(currentIndex); }

   public String getAnswer() { return ""; }
   public void setAnswer(String newValue) { 
      try {
        if (currentIndex == 0) conversation.begin();
         if (getCurrent().getAnswer().equals(newValue) ) {
            
             score=score+20;            
          currentIndex = (currentIndex + 1) % problems.size();
         }
         if (currentIndex == 0) conversation.end();
      }
      catch (Exception ex) {
          System.out.printf(ex.toString());
      }
   }   

}
